package dst1.model;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotNull;

@Entity
@Table(name="USER",
	uniqueConstraints = @UniqueConstraint(columnNames={"USERNAME", "ACCOUNTNO", "BANKCODE"}))
@NamedQueries(
		value={
		   @NamedQuery (
			name = "find_all_users_specific_grid_x_jobs",
			query = "Select u.username, count(j) as c" +
					" from User u join u.jobs j join u.membership m " +
					" where m.grid.name = ? group by u.username  having count(j) >= ? order by c"),
		   @NamedQuery (name="find_most_active_user",
		    query = "select u.username, count(j) as c " +
		    		" from User u join u.jobs j group by u.username order by c desc")
		 }
	)
public class User extends Person{

	@NotNull
	@Column(name="USERNAME") 
	private String username;
	
	//MD5 is 128 bit long
	@Column(name="PASSWORD")
	private byte[] password;
	
	@Column(name="ACCOUNTNO")
	private String accountNo;
	
	@Column(name="BANKCODE")
	private String bankCode;
	
	@OneToMany(cascade=CascadeType.ALL, mappedBy="user", fetch=FetchType.LAZY)
	private List<Membership> membership;

	@OneToMany(cascade=CascadeType.ALL, mappedBy="user", fetch=FetchType.LAZY)	
	private List<Job> jobs;
	
	public User(){
		
	}

	public User(String firstName, String lastName, Address address, 
			String username, byte[] password, List<Membership> membership,
			List<Job> jobs, String accountNo, String bankcode) {
		super(firstName, lastName, address);
		this.username = username;
		this.password = password;
		this.membership = membership;
		this.jobs = jobs;
		this.accountNo = accountNo;
		this.bankCode = bankcode;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public byte[] getPassword() {
		return password;
	}

	public void setPassword(byte[] password) {
		this.password = password;
	}

	public List<Membership> getMembership() {
		return membership;
	}

	public void setMembership(List<Membership> membership) {
		this.membership = membership;
	}

	public List<Job> getJobs() {
		return jobs;
	}

	public void setJobs(List<Job> jobs) {
		this.jobs = jobs;
	}

	public String getAccountNo() {
		return accountNo;
	}

	public void setAccountNo(String accountNo) {
		this.accountNo = accountNo;
	}

	public String getBankCode() {
		return bankCode;
	}

	public void setBankCode(String bankCode) {
		this.bankCode = bankCode;
	}
	
}
